/**
 * 厦门大学计算机专业 | 华为开发专家(HDE)
 * 专注《零基础学编程系列》  http://lblbc.cn/blog
 * 包含：鸿蒙 | Java | 安卓 | 前端 | Flutter | iOS | 小程序
 * 公众号：蓝不蓝编程
 */
package cn.lblbc.numselect

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.lblbc.numselect.ui.theme.NumSelectTheme
import kotlin.random.Random

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            NumSelectTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    RandomNumbers(
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }
    }
}

@Composable
fun RandomNumbers(modifier: Modifier = Modifier) {
    // 使用单独的状态变量存储每个数字
    var num1 by remember { mutableStateOf("0") }
    var num2 by remember { mutableStateOf("0") }
    var num3 by remember { mutableStateOf("0") }
    var num4 by remember { mutableStateOf("0") }
    var num5 by remember { mutableStateOf("0") }
    var num6 by remember { mutableStateOf("0") }
    var num7 by remember { mutableStateOf("0") }

    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp),
            horizontalArrangement = Arrangement.Center
        ) {
            NumberWithRingBackground(num1, Modifier.padding(4.dp))
            NumberWithRingBackground(num2, Modifier.padding(4.dp))
            NumberWithRingBackground(num3, Modifier.padding(4.dp))
            NumberWithRingBackground(num4, Modifier.padding(4.dp))
            NumberWithRingBackground(num5, Modifier.padding(4.dp))
            NumberWithRingBackground(num6, Modifier.padding(4.dp))
            NumberWithRingBackground(num7, Modifier.padding(4.dp))
        }

        Box(modifier = Modifier.height(20.dp))

        Button(
            onClick = {
                val numbers = List(33) { it + 1 }.shuffled().take(6)
                num1 = numbers[0].toString()
                num2 = numbers[1].toString()
                num3 = numbers[2].toString()
                num4 = numbers[3].toString()
                num5 = numbers[4].toString()
                num6 = numbers[5].toString()
                num7 = (Random.nextInt(1, 17)).toString()
            },
            modifier = Modifier
                .width(200.dp)
                .height(50.dp)
                .then(Modifier.align(Alignment.CenterHorizontally)),
            colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF489CF5))
        ) {
            Text(text = "生成", fontSize = 16.sp)
        }
    }
}

@Composable
fun NumberWithRingBackground(number: String, modifier: Modifier = Modifier) {
    Box(
        modifier = modifier
            .width(40.dp)
            .height(40.dp)
            .background(Color.LightGray, shape = CircleShape)
    ) {
        Text(
            text = number,
            modifier = Modifier
                .align(Alignment.Center) // 使文本在Box中居中
                .padding(8.dp), // 根据需要调整内边距以更好地居中文本
            fontSize = 14.sp
        )
    }
}

@Preview(showBackground = true)
@Composable
fun RandomNumbersPreview() {
    NumSelectTheme {
        RandomNumbers()
    }
}